<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				instId: '',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			onSelectYear(){
				this.findTrainYearPlanDataList()
			},
			clearYear(){
				this.$refs.autoform.clear("topinfo","year")
				this.findTrainYearPlanDataList()
			},
			onSelectMonth(){
				this.findTrainYearPlanDataList()
			},
			clearMonth(){
				this.$refs.autoform.clear("topinfo","month")
				this.findTrainYearPlanDataList()
			},
			onSelectLevelType(){
				this.findTrainYearPlanDataList()
			},
			clearLevelType(){
				this.$refs.autoform.clear("topinfo","levelTypeId")
				this.findTrainYearPlanDataList()
			},
			findTrainYearPlanDataList(){
				let _that = this		
				let trainPlanDataShowObj = { 
					year: _that.$refs.autoform.formdata.year,
					month: _that.$refs.autoform.formdata.month,
					levelTypeId: _that.$refs.autoform.formdata.levelTypeId,
					instId: _that.instId
				}
				uni.setStorageSync('trainPlanDataShowKey', trainPlanDataShowObj)				
				if(_that.$refs.autoform.formdata.year && _that.$refs.autoform.formdata.month && _that.$refs.autoform.formdata.levelTypeId){
					_that.om.request('/trainMonthPlan/findTrainYearPlanDataList', trainPlanDataShowObj, 'post').then(res => {
						let dataList = res
						_that.subEditorObj["title1"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
					})
				}else{
					let dataList = []
					this.subEditorObj["title1"].workList = dataList
					this.$refs.autoform.$set(this.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
					this.$set(this.formdata,'dataStr',JSON.stringify(dataList))
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				ajaxArr.push({
					url:"/common/findYearList",
					param:{
						after: 1,
						oldYear: ''
					}
				},{
					url:"/common/findMonthList"
				},{
					url:"/tsysdict/getCombobox",
					param:{
						code: 38813
					}
				})
				if(fileId){
					ajaxArr.push({
						url:"trainMonthPlan/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'), mainData);
					_that.instId = res[0][0].instId
					changeEditor('topinfo', 'year', 'data', configSelectData(res[1], 'value', 'text'), mainData);
					changeEditor('topinfo', 'month', 'data', configSelectData(res[2], 'referNum', 'dicname'), mainData);
					changeEditor('topinfo', 'levelTypeId', 'data', configSelectData(res[3], 'id', 'dicname'), mainData);
					
					changeEditor("title1", "dataStr", {
						titleTemplate: "  <%= projectName %>",
						subtitle1Template: "<%= '培训对象：'+ (trainObj ? trainObj : '') %>",
						subtitle2Template: "<%= '培训类型：'+ (trainTypeName ? trainTypeName : '') %>"
					}, null, mainData)
					
					let trainPlanDataShowObj = {
						year: '',
						month: '',
						levelTypeId:'',
						instId: ''
					}
					uni.setStorageSync('trainPlanDataShowKey', trainPlanDataShowObj)
					if(fileId) {
						let fileIdArr = getFormFields(mainData)
						fileIdArr.push("id")
						for(let index in res[4].value){
							if(fileIdArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value[index])
								_that.$set(_that.formdata,index,res[4].value[index])
							}
						}
						
						let trainPlanDataShowObj = {
							year: res[4].value.year,
							month: res[4].value.month,
							levelTypeId:res[4].value.levelTypeId,
							instId: _that.instId
						}
						uni.setStorageSync('trainPlanDataShowKey', trainPlanDataShowObj)
						
						let dataList = _that._.cloneDeep(res[4].value.dataList)
						_that.subEditorObj["title1"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
						delete res[4].value.dataList
						_that.$nextTick(function(){
							_that.$refs.autoform.changeEditor("topinfo","zposId","type","labelbox")
							_that.$refs.autoform.initSelectIndex('topinfo', 'year')
							_that.$refs.autoform.initSelectIndex('topinfo', 'month')
							_that.$refs.autoform.initSelectIndex('topinfo', 'levelTypeId')
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
					})
				},options)
				
			},
			formSubmit() {
				let _that = this;
				if(_that.edit){
					_that.om.formSave('/trainMonthPlan/doUpdate',_that.formdata,_that)
				}else{
					_that.om.request(
						"posBusiAuth/verifyOne",
						{
							busCode:_that.busCode,
							isNew:false
						}
					).then(res=>{
						if(res.succeed){
							_that.om.formSave('/trainMonthPlan/doAdd',_that.formdata,_that)
						}else{
							_that.om.toast(res.msg)
						}
					})
					
				}
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			for (var fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
